const { Sequelize, Model, Op } = require('sequelize')
const { sequelize } = require('../../core/db')
const { NotFound } = require('../../core/http-exception')
const { Favor } = require('./favor')

class HotBook extends Model {

    // 热门
    static async getAll() {
        const books =await HotBook.findAll({
            order:[
                'index'
            ]
        })
        if(!books){
            throw new NotFound();
        }
        const ids = []
        books.forEach((book)=>{
            ids.push(book.id)
        })
        // 当前数据点赞数量 
        const favors =await Favor.findAll({
            where:{
                art_id:{
                    [Op.in]:ids,
                },
                type:400
                // 国画
                // 漫画
            },
            group:['art_id'],
            attributes:['art_id', [Sequelize.fn('COUNT','*'),'count']]
        })

        books.forEach(book=>{
            HotBook._getEachBookStatus(book,favors)
        })

        return books;

    }

   
    // 添加 点赞数量
    static _getEachBookStatus(book,favors){
        let count = 0;
        favors.forEach(favor=>{
            if(book.id === favor.art_id){
                count = favor.get('count')
            }
        })

        book.setDataValue('fav_nums',count)
        return book;
    }

}

HotBook.init({
    index: Sequelize.INTEGER,
    image: Sequelize.STRING,
    author: Sequelize.STRING,
    title: Sequelize.STRING
}, {
    sequelize,
    tableName: 'hot_book'
})

module.exports = {
    HotBook
}